package com.x.aoki.payment8002.controller;

import com.x.aoki.common.pojo.Payment;
import com.x.aoki.common.utils.CR;
import com.x.aoki.payment8002.service.IPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 支付的主控制器类
 *
 * @author caoy
 * @date 2025-02-14 16:38:16
 */
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private IPaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/payment/create")
    public CR create(@RequestBody Payment payment) {

        int nums = paymentService.create(payment);
        if (nums > 0) {
            return new CR(200, "数据插入成功,serverPort："+serverPort, nums);
        } else {
            return new CR(400, "数据插入失败", null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CR<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        if (!Objects.isNull(payment)) {
            return new CR<>(200, "查询成功,serverPort："+serverPort, payment);
        } else {
            return new CR<>(400, "没有当前 ID:[" + id + "] 对应的记录", null);
        }

    }

    @GetMapping("/payment/lb")
    public String paymentLb(){
        return serverPort;
    }
    @GetMapping("/payment/feign/timeout")
    public String paymentTimeout(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("/payment/zipkin")
    public String paymentZipkin(){
        return "【8002】  = == === zipkin采样啦啊啊啊啊";
    }
}
